package com.uikoo9.manage.y.controller;

import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.weixin.sdk.api.ApiResult;

import com.uikoo9.manage.y.model.YMeetingModel;
import com.uikoo9.manage.y.util.Ret;
import com.uikoo9.manage.y.util.TimeUtil;
import com.uikoo9.util.core.annotation.QControllerUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * 因为涉及到 jfinal-weixin 版本问题，必须依赖 jfinal 3.*以上，但是JfinalQ 只有1.9
 * 所以Meeting单独项目运行，相关代码迁移至 meeging-jfinal
 */
@QControllerUrl("/y/meeting")
public class YmeetingController extends YController  {
    private static final Logger logger = LoggerFactory.getLogger(YMeetingModel.class);

    public static String  _reserve_start_time = "reserve_start_time";
    public static String _reserve_end_time = "reserve_end_time";
    public static String  _start_time = "start_time";
    public static String _end_time = "end_time";
    public static String _select_date_days = "select_date_days";

    public static DbPro getDB(){
        return Db.use("meeting");
    }

    public static Integer getCurrentUserId(){
        return 1;
    }

    public static Ret getConfig(String type){
        Map _result = new HashMap();
        if("config".equals(type)){
            _result.put(_reserve_start_time,"07:00:00");
            _result.put(_reserve_end_time,"22:30:00");
        }else{
            _result.put(_start_time,"07:00:00");
            _result.put(_end_time,"22:30:00");
        }
        _result.put(_select_date_days,19);
        return Ret.create().set(_result);
    }


    /**
     * 发现这个接口 是作者后来加的，并且不是http，而是 webscoket，应该是为了增加迁移成本
     * 还好我有之前的版本，完美
     */
    @ActionKey("/wsapi")
    public void wsapi(){

        Map _result = new HashMap();
        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",_result));

    }

    /**
     * {
     * 	"data": {
     * 		"id": 6468,
     * 		"nickname": "",
     * 		"avatarurl": null,
     * 		"need_refresh": false
     *        },
     * 	"code": 0,
     * 	"message": "返回成功"
     * }
     * 微信 相当于用户认证都帮你做好了，你直接使用用户就行了
     */
    @ActionKey("/api/wechat/login")
    public void wechat_login(){
        String js_code = getPara("js_code");

        //todo 新增 wechat_user，配置角色

//        logger.info("js_code"+js_code);
        Map _result = new HashMap();
        _result.put("id",6468);
        _result.put("nickname","zhangsan");
        _result.put("need_refresh",false);

        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",_result));
    }

    /**
     * 微信用户信息
     */
    @ActionKey("/api/wechat/user/info")
    public void wechat_user_info(){
        String encrypted_data = getPara("encrypted_data");
        String iv = getPara("iv");
        String sessionid = "";


//        logger.info("encrypted_data:"+encrypted_data);
        Map _result = new HashMap();
        _result.put("id",6468);
        _result.put("nickname","zhangsan");
        _result.put("need_refresh",false);

        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",_result));
    }

    /**
     * 会议配置信息
     * {
     * 	"data": {
     * 		"reserve_start_time": "07:00:00",
     * 		"reserve_end_time": "22:30:00",
     * 		"select_date_days": 19,
     * 		"banner_ad_unit_id": "adunit-8bbcd17425bb250c",
     * 		"banner_ad_intervals": 30
     *        },
     * 	"code": 0,
     * 	"message": "返回成功"
     * }
     */
    @ActionKey("/api/meeting/config")
    public void meeting_config(){

//        _result.put("banner_ad_unit_id","adunit-8bbcd17425bb250c");
//        _result.put("banner_ad_intervals",30);

        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",getConfig("config")));

    }

    /**
     * 关注的会议室
     */
    @ActionKey("/api/meeting/follow/rooms")
    public void meeting_follow_rooms(){
        Integer userid = getCurrentUserId();
        List<Record> _result = YMeetingModel.dao.findRooms(null,Ret.create().set("b.user_id",userid));
        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",_result));
    }

    /**
     * 我创建的会议室
     */
    @ActionKey("/api/meeting/create/rooms")
    public void meeting_create_rooms(){
        Integer userid = getCurrentUserId();
        List<Record> _result = YMeetingModel.dao.findRooms(null,Ret.create().set("a.create_user_id",userid));
        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",_result));
    }

    /**
     * 我的会议
     * {"data":{"start_time":"07:00:00","end_time":"22:30:00","start_date":"2019-10-13","end_date":"2019-11-01",
     *          "rooms":[{"id":4,"name":"会议室101","description":"描述334"}],
     *          "meetings":[{"id":6,"user":1,"room":4,"date":"2019-10-14","start_time":"10:30:00","end_time":"12:00:00","name":"ju","description":"111"}]},
     *  "code":0,"message":"返回成功"}
     */
//    @ActionKey("/api/meeting/my/meetings?date=")
    @ActionKey("/api/meeting/my/meetings")
    public void meeting_my_meetings(){
        Date date = getParaToDate("date",new Date());//todo 确定格式
        Integer userid = getCurrentUserId();
        Ret ret = Ret.create().set(getConfig(""));
        ret.set("start_date",TimeUtil.formatDate(date,"yyyy-MM-dd"));
        ret.set("end_date",TimeUtil.formatDate(TimeUtil.addDays(date,getConfig("").getInt(_select_date_days)),"yyyy-MM-dd"));
        //todo 我参加会议的会议室
        List<Record> rooms = new ArrayList<Record>();
        ret.set("rooms",rooms);
        //todo 我参加的会议
        List<Record> meetings = new ArrayList<Record>();
        ret.set("meetings",meetings);
        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",ret));
    }

    /**
     * 新增会议室
     */
//    @ActionKey("/api/meeting/room/create?name= 会议室1012&description= 描述2&create_user_manager= true")
    @ActionKey("/api/meeting/room/create")
    public void meeting_room_create(){
        String name = getPara("name");
        String desc = getPara("description");
        String create_user_manager = getPara("create_user_manager");

    }

    /**
     * 编辑会议室
     */
//    @ActionKey("/api/meeting/room/edit?room_id= 4&name= 会议室101&description= 描述334&create_user_manager= true")
    @ActionKey("/api/meeting/room/edit")
    public void meeting_room_edit(){
        String name = getPara("name");
        String desc = getPara("description");
        String create_user_manager = getPara("create_user_manager");

    }

    /**
     * 会议室明细
     *{"data":
     *      {"id":1,"name":"121","description":"1212","qr_code":null,"create_user":1,"is_follow":true,"create_user_manager":false}
     * ,"code":0,"message":"返回成功"}
     */
//    @ActionKey("/api/meeting/room/info?room_id=1")
    @ActionKey("/api/meeting/room/info")
    public void meeting_room_info(){
        //todo 通过session 获取当前用户信息
        Integer currentUserid = getCurrentUserId();

        Integer room_id = getParaToInt("room_id");
        Record _result = getDB().findFirst("select a.*,(select count(1)>0 from meetings_userfollowroom where room_id = ? and user_id = ? ) is_follow from meetings_room a where id=?",
                room_id,currentUserid,room_id);
        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",_result));
    }

    /**
     * 会议列表
     * {"data":{"start_time":"07:00:00","end_time":"22:30:00","start_date":"2019-10-13","end_date":"2019-11-01",
     *          "rooms":[{"id":4,"name":"会议室101","description":"描述334"},{"id":7,"name":"测试用户新增","description":"测试"}],
     *          "meetings":[{"id":6,"user":1,"room":4,"date":"2019-10-14","start_time":"10:30:00","end_time":"12:00:00","name":"ju","description":"111"}]},
     * "code":0,"message":"返回成功"}
     */
//    @ActionKey("/api/meeting/room/meetings?room_ids=1&date=")
    @ActionKey("/api/meeting/room/meetings")
    public void meeting_room_meetings(){
        Date date = getParaToDate("date",new Date());//todo 确定格式, 展示列表 不变，都是19天，data 用来查询数据
        Integer room_ids = getParaToInt("room_ids");

        Integer userid = getCurrentUserId();
        Ret ret = Ret.create().set(getConfig(""));
        ret.set("start_date",TimeUtil.formatDate(date,"yyyy-MM-dd"));
        ret.set("end_date",TimeUtil.formatDate(TimeUtil.addDays(date,getConfig("").getInt(_select_date_days)),"yyyy-MM-dd"));
        //todo 我参加会议的会议室
        List<Record> rooms = YMeetingModel.dao.findRooms(null,Ret.create().set("b.user_id",userid));
        ret.set("rooms",rooms);
        //todo 我参加的会议
        List<Record> meetings = new ArrayList<Record>();
        ret.set("meetings",meetings);
        renderJson(Ret.ok("code",0).set("message","返回成功").set("data",ret));

    }

    /**
     * 关注的会议室
     */
//    @ActionKey("/api/meeting/room/follow?room_id=5")
    @ActionKey("/api/meeting/room/follow")
    public void meeting_room_follow(){
        Integer room_id = getParaToInt("room_id");

        renderJson();
    }

    /**
     * 取消关注会议室
     */
//    @ActionKey("/api/meeting/room/un/follow?room_id=5")
    @ActionKey("/api/meeting/room/un/follow")
    public void meeting_room_un_follow(){
        Integer room_id = getParaToInt("room_id");

        renderJson();
    }

    /**
     * 删除会议室
     */
//    @ActionKey("/api/meeting/room/delete?room_id=5")
    @ActionKey("/api/meeting/room/delete")
    public void meeting_room_delete(){
        Integer room_id = getParaToInt("room_id");

        renderJson();
    }

    /**
     * 预约会议-新增
     */
//    @ActionKey("/api/meeting/reserve?room_id= 1&name= 3434-key&description= 33&
//      date= 2019-08-07&start_time= 07:00&end_time= 08:00")
    @ActionKey("/api/meeting/reserve" )
    public void meeting_reserve(){
        Integer room_id = getParaToInt("room_id");
        String name = getPara("name");
        String desc = getPara("description");
        Date date = getParaToDate("date");//todo 确定格式
        String start_time = getPara("start_time");
        String end_time = getPara("end_time");


        renderJson();
    }

    /**
     * 编辑会议
     */
//    @ActionKey("/api/meeting/edit?meeting_id= 4&name= 参数测试&description= 测试3355")
    @ActionKey("/api/meeting/edit")
    public void meeting_edit(){
        Integer meeting_id = getParaToInt("meeting_id");
        String name = getPara("name");
        String desc = getPara("description");

        renderJson();
    }

    /**
     * 会议信息
     */
//    @ActionKey("/api/meeting/info?meeting_id=3")
    @ActionKey("/api/meeting/info")
    public void meeting_info(){
        Integer meeting_id = getParaToInt("meeting_id");

        renderJson();
    }

    /**
     * 取消会议
     */
//    @ActionKey("/api/meeting/cancel?meeting_id=3")
    @ActionKey("/api/meeting/cancel")
    public void meeting_cancel(){
        Integer meeting_id = getParaToInt("meeting_id");

        renderJson();
    }

    /**
     * 参加会议
     */
//    @ActionKey("/api/meeting/join?meeting_id=4")
    @ActionKey("/api/meeting/join")
    public void meeting_join(){
        Integer meeting_id = getParaToInt("meeting_id");

        renderJson();
    }







}
